<template>
  <div class="container">
    <div class="app-container">
      <el-button type="primary" class="btn" size="mini" @click="addemployee(0,1)">添加权限</el-button>
      <el-table
        :data="tableData"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        default-expand-all
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column
          prop="name"
          label="名称"
        />
        <el-table-column
          prop="code"
          label="标识"
        />
        <el-table-column
          prop="description"
          label="描述"
        />
        <el-table-column
          prop="address"
          label="操作"
        >
          <template v-slot="{row}">
            <el-button v-if="row.type===1" type="text" size="mini" @click="addemployee(row.id,2)">添加</el-button>
            <el-button type="text" size="mini" @click="addemployee(row.id)">编辑</el-button>
            <el-button type="text" size="mini" @click="remvePremission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <addPermission v-model="dialogVisible" :add-ids="addIds" @uploadSuccess="getPermissionList" />
  </div>
</template>
<script>
import { getPermissionList, remvePremission } from '@/api/permission'
import { zhuanghua } from '@/utils'
import addPermission from './components/add_permission.vue'
export default {
  name: 'Permission',
  components: {
    addPermission
  },
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      addIds: {
        id: 0,
        type: null
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const res = zhuanghua(await getPermissionList(), 0)
      // console.log(res)
      this.tableData = res
    },
    addemployee(id, num) {
      console.log(id, num)
      // 记录父级权限
      this.addIds.id = id
      // 用于记录几级权限
      this.addIds.type = num
      this.dialogVisible = true
    },
    remvePremission(id) {
      this.$confirm('此操作将永久删除该条数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await remvePremission(id)
        this.getPermissionList()
      })
    }
  }
}
</script>
<style lang="scss" scoped>
  .btn{
    margin: 10px;
  }
</style>
